package com.yes.yll.servlet;

import com.yes.yll.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/ylladd")
public class UserAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String n = req.getParameter("n");
        String z = req.getParameter("z");
        String i = req.getParameter("i");

        if(i!=null && !"null".equals(i)){
            //存在id，更新
            String sql="UPDATE `user` SET `name`=?,username=? WHERE id=?";
            JdbcUtils.modify(sql,n,z,i);
        }else{
            // 不存在id 新增
            String sql="INSERT INTO `user`(username,`password`,`name`) VALUES(?,MD5('123456'),?)";
            JdbcUtils.modify(sql,z,n);
        }

        resp.sendRedirect("/yll/user-list.jsp");
    }
}
